package com.snoob.lixing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.snoob.lixing.mapper.ClassesMapper;
import com.snoob.lixing.pojo.Classes;
import com.snoob.lixing.service.ClassesService;
import com.snoob.lixing.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: lixing
 * Date: 2018/11/24 20:36
 */
@Service
public class ClassesServiceImpl implements ClassesService {

    @Resource
    private ClassesMapper classesMapper;

    /**
     * 分页查询
     *
     * @param pojo       分页条件
     * @param pageNumber 分页页码
     * @param pageSize   分页大小
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo page(Classes pojo, Integer pageNumber, Integer pageSize) throws Exception {
        //
        Example example = new Example(Classes.class);
        example.orderBy("createTime").desc();
        //
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isEffective(pojo.getName())) {
            criteria.andLike("name", "%" + pojo.getName() + "%");
        }
        if (pojo.getCreateTime() != null) {
            criteria.andEqualTo("createTime", pojo.getCreateTime());
        }
        if (pojo.getCreateTimeStart() != null) {
            criteria.andGreaterThanOrEqualTo("createTime", pojo.getCreateTimeStart());
        }
        if (pojo.getCreateTimeEnd() != null) {
            criteria.andLessThanOrEqualTo("createTime", pojo.getCreateTimeEnd());
        }
        //
        PageHelper.startPage(pageNumber, pageSize);
        List<Classes> classesList = classesMapper.selectByExample(example);
        PageInfo page = new PageInfo(classesList);
        return page;
    }

    /**
     * 查询
     *
     * @param pojo 查询参数
     * @return
     * @throws Exception
     */
    @Override
    public List<Classes> selectPojo(Classes pojo) throws Exception {
        //
        Example example = new Example(Classes.class);
        example.orderBy("createTime").desc();
        //
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isEffective(pojo.getId())) {
            criteria.andEqualTo("id", pojo.getId());
        }
        if (StrUtil.isEffective(pojo.getName())) {
            criteria.andLike("name", "%" + pojo.getName() + "%");
        }
        if (pojo.getCreateTime() != null) {
            criteria.andEqualTo("createTime", pojo.getCreateTime());
        }
        if (pojo.getCreateTimeStart() != null) {
            criteria.andGreaterThanOrEqualTo("createTime", pojo.getCreateTimeStart());
        }
        if (pojo.getCreateTimeEnd() != null) {
            criteria.andLessThanOrEqualTo("createTime", pojo.getCreateTimeEnd());
        }
        //
        List<Classes> classesList = classesMapper.selectByExample(example);
        return classesList;
    }

    /**
     * 编辑
     *
     * @param pojo id为空为添加、id为空为更新
     * @return
     * @throws Exception
     */
    @Override
    public Object modifyPojo(Classes pojo) throws Exception {
        Integer result;
        if (StrUtil.isEffective(pojo.getId())) {
            /**
             * 编辑
             */
            result = classesMapper.updateByPrimaryKeySelective(pojo);
        } else {
            /**
             * 添加
             */
            result = classesMapper.insertSelective(pojo);
        }
        return result;
    }

    /**
     * 删除
     *
     * @param pojoIdList 集合
     * @return
     * @throws Exception
     */
    @Override
    public Object deletePojo(List<String> pojoIdList) throws Exception {
        Integer result;
        //
        Example example = new Example(Classes.class);
        //
        Example.Criteria criteria = example.createCriteria();
        if (!pojoIdList.isEmpty()) {
            criteria.andIn("id", pojoIdList);
        }
        //
        result = classesMapper.deleteByExample(example);
        return result;
    }

    /**
     * 导入Excel文件
     *
     * @param file   Excel文件
     * @param action 导入指令：insert(批量添加)、update(批量更新)、delete(批量删除)
     * @return
     * @throws Exception
     */
    @Override
    public Object importExcel(MultipartFile file, String action) throws Exception {
        return null;
    }

    /**
     * 导出Excel文件
     *
     * @param pojo     导出条件
     * @param response 响应Excel文件到浏览器
     * @throws Exception
     */
    @Override
    public void exportExcel(Classes pojo, HttpServletResponse response) throws Exception {

    }
}
